#include "DiscountCurve.h"
#include <ctype.h>
#include <cstdlib>
#include <unistd.h>
#include <fstream>

int main(int argc, char **argv)
{
  string datafile = "rates.csv"; 
  string datestr = "2/15/2002"; 
  string count = "act360";
  string curve = "linear";
  DayCounter *counter;
  DiscountCurve *cur;

  // How to change to using OptionParser.h
  char c;
  while ((c = getopt (argc, argv, "hf:d:t:c:")) != -1) {
    switch (c) {
      case 'h':
        cout << "usage: " << argv[0] 
             << " [-h] [-f datafile] [-d mm/dd/yyyy] [-t actact|act360|30360]"
             << " [-c constant|linear]"
             << endl;
        return 0;
      case 'f':
        datafile = string(optarg);
        break;
      case 'd':
        datestr = string(optarg);
        break;
      case 't':
        count = string(optarg);
        break;
      case 'c':
        curve = string(optarg);
        break;
      case '?':
        return 1;
      default:
        abort();
    }
  }
 
  if (count.compare("actact") == 0) {
    counter = new ActualActual;
  } else if (count.compare("30360") == 0) {
    counter = new Thirty360;
  } else {
    counter = new Actual360;
  }
  
  try {
      if (curve.compare("constant") == 0) {
        cur = new DiscountCurveConstant(datafile, *counter);
      } else {
        cur = new DiscountCurveLinear(datafile, *counter);
      }
  }
  catch (exception &e) {
    cout << e.what()<< endl;
    return 1;
  }
  
  Date date(datestr);
  cout << "Given rates in file " << datafile << ", "
       << "the rate by " << count << " counter and " << curve 
       << " curve on " << date << " is " << cur->getRate(date) << endl;
 
  delete cur;
  delete counter;

  return 0;
}
